package com.likai.monitor.controller;



import com.alibaba.fastjson.JSONObject;
import com.likai.monitor.vo.DiskInfo;
import com.likai.monitor.vo.SystemInfo;
import com.sun.management.OperatingSystemMXBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.text.DecimalFormat;


@RequestMapping("/monitor")
@Controller
public class MonitorController {
    DecimalFormat df = new DecimalFormat("#.##");
    OperatingSystemMXBean operatingSystemMXBean= ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);

    @RequestMapping("/monitor")
    public String monitor(){
        return "monitor";
    }

    @RequestMapping("/getSystemInfo")
    @ResponseBody
    public String getSystemInfo() throws Exception {
        SystemInfo systemInfo=new SystemInfo();
        /*系统信息*/
        systemInfo.setSysInfo(System.getProperty("os.name"));
        InetAddress ip4 = Inet4Address.getLocalHost();
        systemInfo.setIp(ip4.getHostAddress());
        /*内存信息*/
        systemInfo.setFreeMemory(Double.parseDouble(df.format(operatingSystemMXBean.getFreePhysicalMemorySize()/1024.0/1024.0/1024.0)));
        systemInfo.setTotalMemory(Double.parseDouble(df.format(operatingSystemMXBean.getTotalPhysicalMemorySize()/1024.0/1024.0/1024.0)));
        systemInfo.setFreeRate(Double.parseDouble(df.format(systemInfo.getFreeMemory()/systemInfo.getTotalMemory()*100)));
        /*CPU信息*/
        systemInfo.setCpuUseRate(Double.parseDouble(df.format(operatingSystemMXBean.getSystemCpuLoad()*100)));
        /*硬盘信息*/
//        List<DiskInfo> diskInfoList=new ArrayList<>();
//        File[] roots = File.listRoots();
//        for (File root:roots){
//            DiskInfo diskInfo=new DiskInfo();
//            diskInfo.setPart(root.getPath());
//            diskInfo.setTotalSpace(Double.parseDouble(df.format(root.getTotalSpace()/1024.0/1024.0/1024.0)));
//            diskInfo.setFreeSpace(Double.parseDouble(df.format(root.getFreeSpace()/1024.0/1024.0/1024.0)));
//            diskInfo.setFreeRate(Double.parseDouble(df.format(diskInfo.getFreeSpace()/diskInfo.getTotalSpace()*100)));
//            diskInfoList.add(diskInfo);
//        }
//        systemInfo.setDiskInfos(diskInfoList);
        String disk="";
        File[] roots = File.listRoots();
        for (File root:roots){
            DiskInfo diskInfo=new DiskInfo();
            diskInfo.setPart(root.getPath());
            diskInfo.setTotalSpace(Double.parseDouble(df.format(root.getTotalSpace()/1024.0/1024.0/1024.0)));
            if (diskInfo.getTotalSpace()<=0){
                continue;
            }
            diskInfo.setFreeSpace(Double.parseDouble(df.format(root.getFreeSpace()/1024.0/1024.0/1024.0)));
            diskInfo.setFreeRate(Double.parseDouble(df.format(diskInfo.getFreeSpace()/diskInfo.getTotalSpace()*100)));

            disk+=diskInfo.getPart()+"   总:"+diskInfo.getTotalSpace()+"GB;剩余:"+diskInfo.getFreeSpace()+"GB:"+diskInfo.getFreeRate()+"%<br/>";

        }
        systemInfo.setDisk(disk);

        return JSONObject.toJSONString(systemInfo);
    }



}
